<?php

namespace app\qsapi\model;

use think\Db;

class Member extends BaseModel
{
    /**
     * 账号注册
     * @param arr $data 创建数据
     * @param int $type 1 = 账号注册 2 = 手机号注册
     * @return 数据信息|mixed
     * @since 2023/08/04
     * @author ZhangXuanNeng
     */
    public function userRegister($data,$type = 1){
        try {
            // 兼容小号注册
            if(isset($data['password']) && !empty($data['password'])){
                $data['password'] = setMd5Pwd($data['password']);
            }
            if($type == 1){
                $field = 'username';
                $msg = '账号';
            }elseif ($type == 2){
                $field = 'phone';
                $data['username'] = $data['phone'];
                $msg = '手机号';
            }else{
                return ['code'=>-1,'msg'=>'未知类型'];
            }
            $status = $this->where($field,$data[$field])->find();
            if($status){
                return ['code'=>-1,'msg'=>$msg.'已存在'];
            }
            $data['register_ip'] = get_client_ip();
            $data['create_time'] = time();
            if(isset($data['promote_id']) && !empty($data['promote_id'])){
                $promoteInfo = Db::name('user_promote')->where('id',$data['promote_id'])->field('realname')->find();
                $data['promote_account'] = $promoteInfo['realname'];
            }
            $id = $this->insertGetId($data);
            return ['code'=>0,'msg'=>'注册成功','data'=>$id];
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }

    }
}